package com.bjsxt.boot;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bjsxt.disruptor.DisruptorTemplate;
import com.bjsxt.disruptor.Order;
import com.bjsxt.domain.EntrustOrder;
import com.bjsxt.mapper.EntrustOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Description:
 * @ClassName: DataLoaderCmdLine
 * @Author: chenyu
 * @Date: 2025/3/20 14:06
 */
@Component
public class DataLoaderCmdLine implements CommandLineRunner {

    @Autowired
    private DisruptorTemplate disruptorTemplate;

    @Autowired
    private EntrustOrderMapper entrustOrderMapper;


    @Override
    public void run(String... args) throws Exception {
        List<EntrustOrder> entrustOrders =
                entrustOrderMapper.selectList(
                        new LambdaQueryWrapper<EntrustOrder>()
                                .eq(EntrustOrder::getStatus, 0)
                                .orderByAsc(EntrustOrder::getCreated)
                );
        if (!CollectionUtils.isEmpty(entrustOrders)) {
            for (EntrustOrder entrustOrder : entrustOrders) {
                disruptorTemplate.onData(entrustOrder2Order(entrustOrder));
            }
        }

    }

    private Order entrustOrder2Order(EntrustOrder entrustOrder) {
        Order order = new Order();
        order.setSymbol(entrustOrder.getSymbol()); // 设置交易对
        order.setAmount(entrustOrder.getVolume().add(entrustOrder.getDeal().negate())); // 设置交易额
        order.setPrice(entrustOrder.getPrice()); // 设置交易价格
        order.setTime(entrustOrder.getCreated().getTime()); // 设置交易时间
        order.setOrderId(entrustOrder.getId().toString()); // 设置交易的id
        order.setOrderDirection(entrustOrder.getType().intValue()); // 设置交易的方向
        return order;
    }
}
